<?php
/**
 * Created by PhpStorm.
 * User: wangxiaodan
 * Date: 2017/5/13
 * Time: 下午11:18
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\TeacherGroups;

class TeacherGroupsController extends Controller
{
    public function index(){
        $groups = TeacherGroups::get();
        return view('admin.teacher_groups.index', compact('groups'));
    }

    public function create(){
        return view('admin.teacher_groups.create');
    }

    public function store(Requests\TeacherGroupsRequest $request){
        $result = TeacherGroups::create($request->all());
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/groups')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }

    public function edit($id){
        $group = TeacherGroups::findOrFail($id);
        return view('admin.teacher_groups.edit', compact('group'));
    }

    public function update(Requests\TeacherGroupsRequest $request, $id){
        $group = TeacherGroups::findOrFail($id);
        $result = $group->update($request->all());
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/groups')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }

    public function destroy($id){
        $group = TeacherGroups::findOrFail($id);
        if ($group->delete()){
            exit(json_encode(array('code'=>1)));
        } else {
            exit(json_encode(array('code'=>0)));
        }
    }
}